package com.guochu.org.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.guochu.common.ResultMap;
import com.guochu.common.annotation.CloseAuth;
import com.guochu.org.entity.User;
import com.guochu.org.service.UserService;
import com.guochu.utils.MapUtils;
import com.guochu.utils.Pagination;
import io.micrometer.core.instrument.util.StringUtils;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * 用户管理表(User)表控制层
 *
 * @author bunting.wei
 * @since 2020-12-11 15:36:19
 */
@RestController
@RequestMapping("org/latAndLngByGaode")
public class LatAndLngByBaiduController {

    @RequestMapping(value = "/latAndLngToCity", method = RequestMethod.POST)
    public ResultMap latAndLngToCity(@RequestBody Map<String, Object> limit) throws IOException {
        String longitude = MapUtils.getString(limit, "longitude");
        String latitude = MapUtils.getString(limit, "latitude");

        String urlString = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=" + longitude + "," + latitude + "&key=397a4611f19a80405402f4436351c204&radius=1000&extensions=all";
        String res = "";
        URL url = new URL(urlString);
        java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("GET");
        java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), "UTF-8"));
        String line;
        while ((line = in.readLine()) != null) {
            res += line + "\n";
        }
        in.close();
        JSONObject jsonObject = JSON.parseObject(res);
        System.out.println(res);
        return ResultMap.success().put("item", jsonObject);
    }
}
